package generator;

import java.io.StringReader;

import junit.framework.TestCase;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class OOImageTest extends TestCase {

    public static void main(String[] args) {
        junit.swingui.TestRunner.run(OpenOfficeDocTest.class);
    }

    protected void setUp() throws Exception {
        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public OOImage getOOImage() throws DocumentException{
        SAXReader saxReader = new SAXReader();
        StringReader reader = new StringReader(value);
        Document document = saxReader.read(reader);
        Element imageEle = document.getRootElement().element("body").element("image");
        assertNotNull(imageEle);
        OOImage ooImage = new OOImage(imageEle);
        return ooImage;
    }

    // the tests...

    public void testIsTag() throws DocumentException {
    	OOImage ooImage = getOOImage();
    	assertTrue(ooImage.isTagged());
    }

    public void testGetTag() throws DocumentException {
    	OOImage ooImage = getOOImage();
    	assertEquals(".*\\_13_.*\\.gif",ooImage.getTag());
    }

    public void testGetName() throws DocumentException {
    	OOImage ooImage = getOOImage();
    	assertEquals("Large Location Map (Layout)",ooImage.getName());
    }

    public void testGetHref() throws DocumentException {
    	OOImage ooImage = getOOImage();
    	assertEquals("#Pictures/10000000000003E800000352A19AD3C6.gif",ooImage.getHref());
    }

    public void testGetFilename() throws DocumentException {
    	OOImage ooImage = getOOImage();
    	assertEquals("Pictures/10000000000003E800000352A19AD3C6.gif",ooImage.getFilename());
    }

    public void testSetFilename() throws DocumentException {
    	OOImage ooImage = getOOImage();
    	ooImage.setFilename(".*\\_13_.*\\.gif");
    	assertEquals("Pictures/.*\\_13_.*\\.gif",ooImage.getFilename());
    }

    public void testSetFilenameHref() throws DocumentException {
    	OOImage ooImage = getOOImage();
    	ooImage.setFilename(".*\\_13_.*\\.gif");
    	assertEquals("#Pictures/.*\\_13_.*\\.gif",ooImage.getHref());
    }

//    public void testOODoc() throws DocumentException, FileNotFoundException, IOException {
//    	OpenOfficeDoc doc = new OpenOfficeDoc();
//    	doc.setContent(value);
//    	doc.getImages();
//    	Compiler.removePlaceholderPictures2(doc);
//
//    	List images = doc.getImages();
//
//    	Iterator it = images.iterator();
//    	while (it.hasNext()){
//			Element element = (Element) it.next();
//			OOImage image = new OOImage(element);
//			assertEquals("#Pictures/.*\\_13_.*\\.gif",image.getHref());
//    	}
//    	doc.setContent();
//    	OpenOfficeDocManager.dumpContent(doc,"imagetest");
//    }
//
//
//    public void testRealThang() throws IOException, DocumentException {
//        OpenOfficeDoc doc = new OpenOfficeDoc();
//        OpenOfficeDocManager.read(doc, "templates/aelssr/aelssr.stw");
//
//    	Compiler.removePlaceholderPictures2(doc);
//    	doc.setContent();
//        OpenOfficeDocManager.dumpContent(doc,"imagetest2");
//    }

    private static String value;

    static {
    	value = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	    	+ "<office:document-content "
	    	+ "xmlns:office=\"http://openoffice.org/2000/office\" "
	    	+ "xmlns:style=\"http://openoffice.org/2000/style\" "
	    	+ "xmlns:text=\"http://openoffice.org/2000/text\" "
	    	+ "xmlns:table=\"http://openoffice.org/2000/table\" "
	    	+ "xmlns:draw=\"http://openoffice.org/2000/drawing\" "
	    	+ "xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" "
	    	+ "xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
	    	+ "xmlns:number=\"http://openoffice.org/2000/datastyle\" "
	    	+ "xmlns:svg=\"http://www.w3.org/2000/svg\" "
	    	+ "xmlns:chart=\"http://openoffice.org/2000/chart\" "
	    	+ "xmlns:dr3d=\"http://openoffice.org/2000/dr3d\" "
	    	+ "xmlns:math=\"http://www.w3.org/1998/Math/MathML\" "
	    	+ "xmlns:form=\"http://openoffice.org/2000/form\" "
	    	+ "xmlns:script=\"http://openoffice.org/2000/script\" "
	    	+ "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" " // added by JN
	    	+ "office:class=\"text\" "
	    	+ "office:version=\"1.0\" "
	    	+ ">"
	        + "<office:script/>"
	        + "<office:body>"
	        + "<draw:image draw:style-name=\"fr6\" "
	        + "draw:name=\"Large Location Map (Layout)\" "
//	        + "draw:name=\"#.*\\_13_.*\\.gif#\" "
	        + "text:anchor-type=\"paragraph\" "
	        + "svg:x=\"5.05cm\" "
	        + "svg:y=\"0.349cm\" "
	        + "svg:width=\"9.999cm\" "
	        + "svg:height=\"8.481cm\" "
	        + "draw:z-index=\"1\" "
	        + "xlink:href=\"#Pictures/10000000000003E800000352A19AD3C6.gif\" "
	        + "xlink:type=\"simple\" "
	        + "xlink:show=\"embed\" "
	        + "xlink:actuate=\"onLoad\">"
	        + "<svg:desc>#.*\\_13_.*\\.gif#</svg:desc>"
	        + "</draw:image>"
	        + "</office:body>"
	        + "</office:document-content>";
    }

}
